<?php
/**
 * Created by PhpStorm.
 * User: UGLY
 * Date: 2018/4/28
 * Time: 10:38
 */

namespace app\blog\controller;


use think\Db;
use cmf\controller\HomeBaseController;
use think\Request;
use think\Validate;

class ListController extends HomeBaseController
{
    private $datebase;
    private $requset;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->datebase = Db::name('simplearticle');
        $this->requset = request();
    }
    public function __destruct()
    {
        unset($this->datebase);
        unset($this->request);
    }

    public function getDatebase()
    {
        return $this->datebase;
    }

    public function getRequest()
    {
        return $this->request;
    }
    //简文
    public function listwork()
    {
        if($this->getRequest()->isGet())
        {
            $simpleArticleList = $this->getDatebase()->order('jw_id','desc')->limit(6)->select();
            $lastArray = end($simpleArticleList);
            $this->assign(['title'=>'龙窝Luck - 简文','data'=>$simpleArticleList,'lastid'=>$lastArray[5]['jw_id']]);
            return $this->fetch();
        }else{
            echo '不接受非同源请求';
        }
    }

    //点击加载更多
    public function clickLoadMore()
    {
        if($this->getRequest()->isPost())
        {
            $returnLastID = $this->getRequest()->post('lastID');
            if($returnLastID != 1)
            {
                $lastID = $returnLastID - 7;
                $clickSimpleArticle = $this->getDatebase()->limit($lastID,6)->select();
//                echo '<pre>';
//                var_dump($clickSimpleArticle);
//                die();
                $data = [
                    'status_id'=>200,
                    'text'=>'加载成功',
                    'data'=>$clickSimpleArticle,
                ];
                echo json_encode($data);
            }
        }else{
            echo '不接受非同源请求';
        }
    }

    //简文详情
    public function listcontent()
    {
        if($this->getRequest()->isGet())
        {
            $getID = input('id');
            $simpleArticle = $this->getDatebase()->where(['jw_id'=>$getID])->find();
            $this->getDatebase()->where(['jw_id'=>$getID])->update(['jw_view'=>$simpleArticle['jw_view']+1]);
            $this->assign(['title'=>'龙窝Luck - ','data'=>$simpleArticle,'ID'=>$getID]);
            return $this->fetch();
        }else{
            echo '不接受非同源请求';
        }
    }

    public function writeSimpleArticle()
    {
        $this->assign(['title'=>'龙窝Luck - 书写简文']);
        return $this->fetch();
    }

    public function addSimpleArticle()
    {
        if($this->getRequest()->isAjax()){
            $post = $this->getRequest()->post();
            //要验证的字段
            $rule = [
                'jw_content' => 'require|max:4000',
                'jw_name'=> 'require|max:10',
                'jw_title'=> 'require|max:50',
            ];
            //提交的数据
            $data = [
                'jw_name'=>$post['jw_name'],
                'jw_content'=>$post['jw_content'],
                'jw_title'=>$post['jw_title'],
                'jw_time'=>date("Y-m-d H:i:s",time()),
                'jw_ip'=>$post['jw_ip'],
                'jw_area'=>$post['jw_area'],
                'jw_protect'=>$post['jw_protect']|0,
            ];
            //出错的信息
            $msg = [
                'jw_name.require' => '请输入简文作者',
                'jw_name.max'     => '名字最多不能超过10个字',
                'jw_title.require' => '请输入简文标题',
                'jw_title.max'     => '简文标题最多不能超过20个字',
                'jw_content.require' => '请输入简文内容',
                'jw_content.max'     => '简文内容最多不能超过2000字',

            ];
            $validate = new Validate($rule,$msg);
            if($validate->check($data)) {
                if ($this->getDatebase()->insert($data)) {
                    $code = [
                        'status_id' => 200,
                        'text' => '提交成功！'
                    ];
                    echo json_encode($code);
                } else {
                    $code = [
                        'status_id' => 500,
                        'text' => '服务器繁忙，稍后重试！'
                    ];
                    echo json_encode($code);
                }
            } else {
                echo json_encode($validate->getError());
            }
        }else{
            echo '不接受非同源请求。';
        }
    }
}